#include "localSiftVocabulary.h"

using namespace std;
using namespace cv;

localSiftVocabulary::localSiftVocabulary(int _size_dictionary,char *_filename, Mat _image_data){
	this->size_dictionary = _size_dictionary;
	this->filename = _filename;
	this->image_data = _image_data;
	
}

void localSiftVocabulary::generateDictionary(){
	// Generating dense keypoint
	float initFeatureScale=1.f; 
	int featureScaleLevels=1;
    float featureScaleMul=0.1f;
    int initXyStep=6;
	int initImgBound=0;
    bool varyXyStepWithScale=true;
    bool varyImgBoundWithScale=false;
	
	DenseFeatureDetector detector(initFeatureScale,featureScaleLevels,featureScaleMul,initXyStep,initImgBound,varyXyStepWithScale,varyImgBoundWithScale);
	vector<KeyPoint> denseQueryKeypoints;
	detector.detect(this->image_data, denseQueryKeypoints);

	//std::cout << "Dense Keypoint" << endl;

	// Generating descriptors
	SiftDescriptorExtractor extractor;
    Mat siftDescriptors;
	extractor.compute(this->image_data, denseQueryKeypoints, siftDescriptors);

	// Building vocabulary
	cv::TermCriteria tc(CV_TERMCRIT_ITER,100,0.001);
	int retries = 1; 
	int flags = KMEANS_PP_CENTERS;
	cv::BOWKMeansTrainer bowTrainer(this->size_dictionary,tc,retries,flags);

	this->dictionary = bowTrainer.cluster(siftDescriptors);
	//cout << "Finishing cluster!" << endl;
	
	
}

Mat localSiftVocabulary::getDictionary(){
	return this->dictionary;
}

int localSiftVocabulary::getSizeDictionary(){
	return this->size_dictionary;
}